/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuitionmanagement.DAO;

import tuitionmanagement.util.PassEncrypt;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import tuitionmanagement.entity.RoleFunction;
import tuitionmanagement.util.DBUtil;
import java.sql.CallableStatement;

/**
 *
 * @author lab
 */
public class RoleFunctionDAO {

    private Connection con;
    //private Connect db;
    // private DBUtil db=new DBUtil();
    private PassEncrypt pe;
    private ResultSet rs;
    private CallableStatement cst;
    private PreparedStatement pst;
    private String lastError;
 
    private final String SQL_READ;
    private final String SQL_UPDATE;
    private final String SQL_DELETE;
   

    public RoleFunctionDAO() {
        con = null;
        pe = new PassEncrypt();
        rs = null;
        pst = null;
        lastError = "";        
        SQL_READ = "SELECT * FROM RoleFunction";
        SQL_UPDATE = "SELECT * FROM RoleFunction WHERE RoleFunctionId = ?";
        SQL_DELETE = "DELETE FROM RoleFunction WHERE Username = ?";
       
    }

    public ArrayList<RoleFunction> readByAll() {
        ArrayList<RoleFunction> list = new ArrayList<RoleFunction>();
        con = DBUtil.getConn();
        try {
            pst = con.prepareStatement(SQL_READ);
            rs = pst.executeQuery();
            while (rs.next()) {
                RoleFunction b = new RoleFunction();              
                b.setRoleFunctionId(rs.getInt(2));
                 b.setFunctionId(rs.getInt(2));
                  b.setRoleFunctionId(rs.getInt(2));
                   b.setRoleFunctionId(rs.getInt(2));
               

                list.add(b);
            }
        } catch (SQLException ex) {
            setLastError(ex.getMessage());
        }

        return list;
    }

    /**
     * Check whether Account is exist?
     * @param b
     * @return boolean
     */
    /**
     * Add a new Account
     * @param Account
     * @return
     */
    public boolean create(RoleFunction rol){
    return true;
    }
    /**
     * Update Account information
     * @param b
     * @return
     */
    public boolean update(RoleFunction rol) {
        con = DBUtil.getConn();
        try {
            pst = con.prepareStatement(SQL_UPDATE, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            pst.setInt(1, rol.getRoleFunctionId());
            rs = pst.executeQuery();
            rs.first();
            rs.updateInt(2, rol.getRoleFunctionId());
            pst.setInt(2, rol.getFunctionId());
            pst.setInt(3, rol.getRoleId());
            rs.updateRow();
            DBUtil.closeConn(con);
            setLastError("Update Account successfully!");
            return true;
        } catch (SQLException ex) {
            setLastError(ex.getMessage());
        } finally {
            DBUtil.closeConn(con);
        }
        return false;
    }

    /**
     * Delete a Account
     * @param username
     * @return
     */
    public boolean delete(String username) {
        con = DBUtil.getConn();
        try {
            pst = con.prepareStatement(SQL_DELETE, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            pst.setString(1, username);
            if (pst.executeUpdate() > 0) {
                setLastError("Delete Customer successfully!");
                return true;
            }
        } catch (SQLException ex) {
            setLastError(ex.getMessage());
        } finally {
            DBUtil.closeConn(con);
        }
        return false;
    }

    /**
     * Check for login
     * @param acc
     * @return boolean
     */
   
    /**
     * Charge password for current account
     * @param acc
     * @param oldPass
     * @return boolean
     */
  
    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }
}
